﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class dashboardOrders : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pane_orders.Text = "Orders Overview";
            this.pp_neworderslector.Text = "Filter";


            if (!IsPostBack)
            {
                ddlnewOrderSelector.Items.Clear();
                ddlnewOrderSelector.Items.Add(new ListItem("Today", "0"));
                ddlnewOrderSelector.Items.Add(new ListItem("Yesterday", "1"));
                ddlnewOrderSelector.Items.Add(new ListItem("This Week", "2"));
                ddlnewOrderSelector.Items.Add(new ListItem("This Month", "3"));
                ddlnewOrderSelector.Items.Add(new ListItem("Custom Dates", "4"));
                ddlnewOrderSelector.SelectedIndex = 4;

                calStartDate.SelectedDate = DateTime.Today.AddDays(-7);
                calEndDate.SelectedDate = DateTime.Today;
            }

            BindData();
        }

        protected void BindData()
        {
            // get collection values
            Commerce.Common.OrderCollection orders = null;

            //Assmeble start and end dates. 

            DateTime startDate;
            DateTime endDate = DateTime.Now;

            // Orders CSV (as displayed in the grid - WYSIWYG)
            csvLink.NavigateUrl = String.Format("ordersCSV.aspx?f={0}&sd={1}&ed={2}", 
                            ddlnewOrderSelector.SelectedIndex,
                            calStartDate.SelectedDate.ToShortDateString(),
                            calEndDate.SelectedDate.ToShortDateString());

            // Order details CSV - outputs the orders with detail of item/price, etc.
            csvLink2.NavigateUrl = String.Format("orderDetailsCSV.aspx?f={0}&sd={1}&ed={2}",
                           ddlnewOrderSelector.SelectedIndex,
                           calStartDate.SelectedDate.ToShortDateString(),
                           calEndDate.SelectedDate.ToShortDateString());
            
            // Order Quickbooks to CSV - outputs the orders for Quickbooks
            csvLink3.NavigateUrl = String.Format("OrdersToItemsCSV.aspx?f={0}&sd={1}&ed={2}",
                           ddlnewOrderSelector.SelectedIndex,
                           calStartDate.SelectedDate.ToShortDateString(),
                           calEndDate.SelectedDate.ToShortDateString());


            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;

            //int lastDayOfThisMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1).Day;

            panelStartDate.Visible = false;
            panelEndDate.Visible = false;

            switch (ddlnewOrderSelector.SelectedValue)
            {
                case "1":// Yesterday
                    startDate = new DateTime(year, month, day).AddDays(-1);
                    endDate = new DateTime(year, month, day, 23, 59, 59).AddDays(-1);

                    break;

                case "2": //This week from Monday

                    DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;
                    int daysOffset = dayOfWeek - DayOfWeek.Monday;
                    DateTime start = DateTime.Now.AddDays(-daysOffset);

                    startDate = new DateTime(start.Year, start.Month, start.Day);
                    endDate = new DateTime(year, month, day, 23, 59, 59);
                    break;

                case "3": //This month
                    startDate = new DateTime(year, month, 1);
                    endDate = new DateTime(year, month, day, 23, 59, 59);
                    break;

                case "4": //Custom dates
                    panelStartDate.Visible = true;
                    panelEndDate.Visible = true;
                    startDate = calStartDate.SelectedDate;
                    endDate = calEndDate.SelectedDate;
                    break;

                default:// Today
                    startDate = new DateTime(year, month, day);
                    endDate = new DateTime(year, month, day, 23, 59, 59);

                    break;
            }


            orders = new OrderCollection().Where("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed).Where(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, startDate).Where(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, endDate).OrderByDesc("OrderDate").Load();

            gvOrders.DataSource = orders;
            gvOrders.DataBind();
        }

        protected void calStartDate_SelectionChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void calEndDate_SelectionChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ddlnewOrderSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

    }
}